package com.ivncorp.evnproject.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.ivncorp.evnproject.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

public class ImageSelectedAdapter extends BaseAdapter {

	ArrayList<String> imageUrls;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;
	Activity activity;
	int count = 0;
	SparseBooleanArray mSparseBooleanArray;

	public ImageSelectedAdapter(Activity activity, ArrayList<String> _imageUrls) {
		this.imageUrls = _imageUrls;
		this.activity = activity;
		mSparseBooleanArray = new SparseBooleanArray();
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_action_camera)
				.showImageForEmptyUri(R.drawable.ic_action_group)
				.showImageOnFail(R.drawable.ic_action_person)
				.cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	public void changeModelList(ArrayList<String> data) {
		this.imageUrls = data;
		notifyDataSetChanged();
	}

	public ArrayList<String> getCheckedItems() {
		ArrayList<String> mTempArry = new ArrayList<String>();

		for (int i = 0; i < imageUrls.size(); i++) {
			if (mSparseBooleanArray.get(i)) {
				mTempArry.add(imageUrls.get(i));
			}
		}

		return mTempArry;
	}

	@Override
	public int getCount() {
		return imageUrls.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	static class ViewHolder {
		ImageView imageView;
		ProgressBar progressBar;
		ImageButton ibtndelete;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		View view = convertView;
		if (view == null) {
			view = activity.getLayoutInflater().inflate(
					R.layout.item_grid_image_selected, parent, false);
			holder = new ViewHolder();
			assert view != null;
			holder.imageView = (ImageView) view.findViewById(R.id.image);
			// holder.tvcount = (TextView) view.findViewById(R.id.tvcount);
			holder.ibtndelete = (ImageButton) view
					.findViewById(R.id.ibtn_delete);
			holder.progressBar = (ProgressBar) view.findViewById(R.id.loading);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		holder.ibtndelete.setOnClickListener(new OnClickListenerButton(position));
		imageLoader.displayImage(imageUrls.get(position), holder.imageView,
				options, new SimpleImageLoadingListener() {
					@Override
					public void onLoadingStarted(String imageUri, View view) {
						holder.progressBar.setProgress(0);
						holder.progressBar.setVisibility(View.VISIBLE);
					}

					@Override
					public void onLoadingFailed(String imageUri, View view,
							FailReason failReason) {
						holder.progressBar.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingComplete(String imageUri, View view,
							Bitmap loadedImage) {
						holder.progressBar.setVisibility(View.GONE);
					}
				}, new ImageLoadingProgressListener() {
					@Override
					public void onProgressUpdate(String imageUri, View view,
							int current, int total) {
						holder.progressBar.setProgress(Math.round(100.0f
								* current / total));
					}
				});

		return view;
	}
	class OnClickListenerButton implements OnClickListener{

		int position;
		public OnClickListenerButton(int position) {
			// TODO Auto-generated constructor stub
			this.position=position;
		}
		
		@Override
		public void onClick(View v) {
			imageUrls.remove(position);
			changeModelList(imageUrls);
			
		}
		
	}

}